package com.songcha.module_bookdetail.ui.activity.detail

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.mvvm.base.BaseViewModel
import com.songcha.library_business.bean.book.BookDetailBean
import com.songcha.library_business.bean.book.BookDetailNetBean
import com.songcha.library_business.proxy.database.BookBrowseHistoryDaoManager
import com.songcha.library_business.proxy.database.BookDetailDaoManager
import com.songcha.library_business.proxy.database.BookShelfDaoManager
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.module_bookdetail.bean.BookCommentListBean

class BookDetailViewModel(app: Application):BaseViewModel<BookDetailRepository>(app) {
    var bookId=""
    var isInBookShelf=MutableLiveData(false)
    val bookDetail=MutableLiveData<BookDetailNetBean.DataBean>()
    val bookCommentList=MutableLiveData<BookCommentListBean.DataBean>()
    var hadShowBookReadTip=false
    var isToBookReader=false

    fun saveBookBrowse(){
        BookBrowseHistoryDaoManager.addBrowseHistory(bookId)
    }

    fun getHadShowBookReadTip(){
        hadShowBookReadTip= SharedPreferenceHelper.getBoolean("bookdetail","show_bookread_tip")
    }

    fun setHadShowBookReadTip(){
        SharedPreferenceHelper.setBoolean("bookdetail","show_bookread_tip",true)
        hadShowBookReadTip=true
    }

    fun saveBookDetail(){
        if(bookDetail.value==null) return
        val detail=BookDetailBean()
        detail.id=bookId
        detail.bookName=bookDetail.value!!.bookName
        detail.author=bookDetail.value!!.author
        detail.desc=bookDetail.value!!.description
        detail.chapterCount=bookDetail.value!!.totalChapters
        if(detail.chapterCount<1)  detail.chapterCount=1
        detail.textCount=bookDetail.value!!.words
        detail.thumb=bookDetail.value!!.cover
        detail.status=bookDetail.value!!.status
        detail.cid=bookDetail.value!!.cid
        detail.isVip=bookDetail.value!!.isVip
        detail.channelId=bookDetail.value!!.channelId
        detail.update=bookDetail.value!!.updateTime

        BookDetailDaoManager.addBookDetail(detail)
    }

    fun addBookToShelf(){
        if(BookShelfDaoManager.addToBookShelf(bookId)){
            isInBookShelf.value=true
        }
    }

    fun removeBookFromShelf(){
        BookShelfDaoManager.removeFromBookShelf(bookId)
        isInBookShelf.value=false
    }

    fun getIsBookInShelf(){
        isInBookShelf.value=BookShelfDaoManager.isInBookShelf(bookId)
    }

    fun getBookdetail(){
        handleApiDataObserver(repository!!.getBookDetail(bookId),object :IApiResult<BookDetailNetBean>{
            override fun onSuccess(bean: BookDetailNetBean) {
                onLoadSuccess()
                bookDetail.value=bean.data
            }

            override fun onError(e: Throwable) {

            }
        },true)
    }

    fun getBookComment(){
        handleApiDataObserver(repository!!.getBookCommentList(bookId,1,3),object :IApiResult<BookCommentListBean>{
            override fun onSuccess(bean: BookCommentListBean) {
                bookCommentList.value=bean.data
            }

            override fun onError(e: Throwable) {

            }
        })

    }


}